using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.Rendering;

namespace asap.lightmap
{
    public class DLightmap : MonoBehaviour
    {
        [Serializable]
        public class DLightmapInfo
        {
            public LightmapsMode lightmapsMode;
            public Texture2D[] lightmapColor;
            public Texture2D[] lightmapDir;
            public Texture2D[] shadowMask;
            public Cubemap envReflection;
            public RendererInfo[] rendererInfos;
            public ReflectionProbeInfo[] reflectionInfos;
            public SphericalHarmonicsL2[] lightProbes;

            public LightmapData[] GetLighmapDatas()
            {
                if(lightmapColor == null || lightmapColor.Length == 0)
                {
                    Debug.LogError("No lightmap info found");
                    return null;
                }

                LightmapSettings.lightmapsMode = lightmapsMode;

                var lightmapdatas = new LightmapData[lightmapColor.Length];

                for(int i = 0; i < lightmapdatas.Length; i++)
                {
                    var lightmapData = new LightmapData();
                    lightmapData.lightmapColor = lightmapColor[i];
                    if(lightmapsMode != LightmapsMode.NonDirectional)
                    {
                        lightmapData.lightmapDir = lightmapDir[i];
                    }
                    if(shadowMask.Length > 0)
                    {
                        lightmapData.shadowMask = shadowMask[i];
                    }

                    lightmapdatas[i] = lightmapData;
                }

                return lightmapdatas;
            }

            public void ApplyRendererInfo()
            {
                if(rendererInfos == null || rendererInfos.Length ==0)
                {
                    Debug.LogError("No Renderer info found");
                    return;
                }

                foreach(var info in rendererInfos)
                {
                    var renderer = info.renderer;
                    renderer.lightmapIndex = info.lightmapIndex;
                    //if(!renderer.isPartOfStaticBatch)
                        renderer.lightmapScaleOffset = info.lightmapOffsetScale;
                }

                if(reflectionInfos != null && reflectionInfos.Length > 0)
                {
                    foreach(var info in reflectionInfos)
                    {
                        var probe = info.probe;
                        probe.mode = ReflectionProbeMode.Custom;
                        probe.customBakedTexture = info.texture;
                    }
                }

                if(lightProbes != null && LightmapSettings.lightProbes != null)
                    LightmapSettings.lightProbes.bakedProbes = lightProbes;

                if(envReflection != null)
                {
                    RenderSettings.defaultReflectionMode = DefaultReflectionMode.Custom;
                    RenderSettings.customReflection = envReflection;
                }
            }
        }

        [Serializable]
        public class RendererInfo
        {
            public Renderer renderer;
            public int lightmapIndex;
            public Vector4 lightmapOffsetScale;
        }

        [Serializable]
        public class ReflectionProbeInfo
        {
            public ReflectionProbe probe;
            public Texture texture;
        }

        [SerializeField]
        public DLightmapInfo lightmapInfo;

        [SerializeField]
        public GameObject root;

        [ContextMenu("LoadLightMap")]
        public void LoadLightMap()
        {
            if(lightmapInfo == null)
                return;
            LightmapSettings.lightmaps = lightmapInfo.GetLighmapDatas();
            lightmapInfo.ApplyRendererInfo();
        }
    }
}
